#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_openal.h"

/*//. Actual OpenAL Class Methods
  ../ There be sounds here. .//*/

PHP_METHOD(OpenAL, init) {
	openal_init();	
	if(openal_error_exists()) { RETURN_FALSE; }
	else { RETURN_TRUE; }
}

PHP_METHOD(OpenAL, shutdown) {
	openal_shutdown();
	if(openal_error_exists()) { RETURN_FALSE; }
	else { RETURN_TRUE; }	
}

PHP_METHOD(OpenAL, errorExists) {
	if(openal_error_exists()) {
		RETURN_TRUE;
	} else {
		RETURN_FALSE;
	}
}

//. Get/Set Listener Gain.

PHP_METHOD(OpenAL, getListenerGain) {
	ALfloat vol;

	alGetListenerf(AL_GAIN, &vol);
	RETURN_DOUBLE((double)vol);
}

PHP_METHOD(OpenAL, setListenerGain) {
	double vol;
	
	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"d",&vol) == FAILURE) {
		return;
	}

	alListenerf(AL_GAIN, (ALfloat)vol);
}

PHP_METHOD(OpenAL, getListenerOrientation) {
	ALfloat pos[6] = {0,0,0,0,0,0};
	int a;

	alGetListenerfv(AL_ORIENTATION, pos);
	
	array_init(return_value);
	for(a = 0; a < 6; a++) {
		add_next_index_double(return_value, (double)pos[a]);
	}
}

PHP_METHOD(OpenAL, setListenerOrientation) {
	double x1,y1,z1,x2,y2,z2;

	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"dddddd",&x1,&y1,&z1,&x2,&y2,&z2) == FAILURE) {
		return;
	}

	ALfloat pos[6] = {
		(ALfloat)x1,
		(ALfloat)y1,
		(ALfloat)z1,
		(ALfloat)x2,
		(ALfloat)y2,
		(ALfloat)z2
	};	
	
	alListenerfv(AL_ORIENTATION, pos);
}

PHP_METHOD(OpenAL, getListenerPosition) {
	ALfloat pos[3] = {0,0,0};
	int a;
	
	alGetListenerfv(AL_POSITION, pos);
	
	array_init(return_value);
	for(a = 0; a < 3; a++) {
		add_next_index_double(return_value, (double)pos[a]);
	}
}

PHP_METHOD(OpenAL, setListenerPosition) {
	double x,y,z;

	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd", &x,&y,&z) == FAILURE) {
		return;
	}
	
	alListener3f(AL_POSITION, (ALfloat)x, (ALfloat)y, (ALfloat)z);
}

PHP_METHOD(OpenAL, getListenerVelocity) {
	ALfloat pos[3] = {0,0,0};
	int a;
	
	alGetListenerfv(AL_VELOCITY, pos);
	
	array_init(return_value);
	for(a = 0; a < 3; a++) {
		add_next_index_double(return_value, (double)pos[a]);
	}
}

PHP_METHOD(OpenAL, setListenerVelocity) {
	double x,y,z;

	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ddd", &x,&y,&z) == FAILURE) {
		return;
	}
	
	alListener3f(AL_VELOCITY, (ALfloat)x, (ALfloat)y, (ALfloat)z);
}
